<?php
/**
 * @package html
 */

require_once "_htmlbase.php";

/**
 * Class which implements all usefull methods used by any offer.
 * In all four type of view - admin, thumbnail, list and full (one offer)
 */
abstract class HtmlOfferBase extends HtmlBase {
	/**
	 * Chooses according to preffered setup or from url what type of view should be displayed
	 * for HtmlOfferList and HtmlOfferThumbnail and HtmlOfferAdmin
	 *
	 * @param string $querySpec - query specification
	 * @param bool $displayErrors - if display errors using Error framework
	 */
	public static function pickHtmlOfferInitializer($querySpec = null, $displayErrors = false) {
		if (Policy::isAdminView()) {
			$typeOfView = Constants::REQ_TYPEOFVIEW_ADMIN;
		} elseif (@!isset($_GET[Constants::REQ_TYPEOFVIEW])) {
    		$typeOfView = SiteContext::getSiteContext()->getPreferredView();
    	} else {
    		$typeOfView = $_GET[Constants::REQ_TYPEOFVIEW];
    		SiteContext::getSiteContext()->setPreferredView($typeOfView);
    	}
    	if ($typeOfView == Constants::REQ_TYPEOFVIEW_ADMIN) {
    		HtmlOfferAdmin::init($querySpec, $displayErrors);
	    	} elseif ($typeOfView == Constants::REQ_TYPEOFVIEW_THUMB) {
			HtmlOfferThumbnail::init($querySpec, $displayErrors);
		} else {
			HtmlOfferList::init($querySpec, $displayErrors);
		}
    }
    
    /**
     * This method will be the same for all offers so its implemented here.
	 * @see HtmlBase::fetchDbObject
	 */
    protected function fetchDbObject(resource $resource) {
		$this->dbObject = Offer::mysqlFetch($resource);
		return $this->dbObject;
	}
	
	/**
	 * Gather all necessary data for creating working pagination.
	 * 
	 * @see HtmlBase::fetchDataForPagesCounting
	 * 
	 * Following class static members are filled:
	 * @see HtmlBase::$countOfPages
	 * @see HtmlBase::$currentPage
	 * @see HtmlBase::$hrefPages
	 * @see HtmlBase::$queryLimit
	 * 
	 * @param int $countInView - desired count of entities on one page
	 * @param string $querySpec - specification of select query
	 * @param string $tableName - name of the table to select from
	 */
	public static function fetchDataForPagesCounting(int $countInView, $querySpec, $tableName = null) {
		return parent::fetchDataForPagesCounting($countInView, $querySpec, 'offers');
	}
	
	/**
	 * Url link to an offer with id $id.
	 * 
	 * @param string|int #$id - id of offer
	 * @return string - url
	 */
	public static function hrefToOffer($id) {
		if (Policy::isAdminView()) {
			return "/?".Constants::REQ_ACTION."=".Constants::REQ_ACTION_OFFEREDIT
					."&amp;".Constants::REQ_TYPE."=".$id;
		}
		
		$seoUrl = SiteSeo::getSiteSeo()->offerIdToUrl($id);
		
		if ($seoUrl != null) {
			return $seoUrl;
		} else {
			return "/?".Constants::REQ_ACTION."=".Constants::REQ_ACTION_OFFER
					."&amp;".Constants::REQ_TYPE."=".$id;
		}
	}
}
?>